# coding=utf-8
import allure
from faker import Faker
from selenium import webdriver
from selenium.webdriver.common.by import By

from homework_web_po.base_page.base_page import BasePage
from homework_web_po.po.address_book_page import AddressBookPage


class AddMengerPage(BasePage):

    _NAME_LOCATOR = By.XPATH, "//*[@id='username']"
    _MID_LOCATOR = By.XPATH, "//*[@id='memberAdd_acctid']"
    _PHONE_LOCATOR = By.XPATH, "//*[@id='memberAdd_phone']"
    _SAVE_BTN = By.XPATH, "//*[text()='保存']"
    _CANCEL_BTN = By.XPATH, "//*[text()='取消']"
    _FAIL_TIPS = By.CSS_SELECTOR,'.member_edit_item_right.ww_inputWithTips_WithErr div'

    @allure.step("添加成员成功")
    def add_menger_save(self,name,mid,phone_num):
        self.input(*self._NAME_LOCATOR,name)
        self.input(*self._MID_LOCATOR,mid)
        self.input(*self._PHONE_LOCATOR,phone_num)
        self.click(*self._SAVE_BTN)
        print("添加成员成功，返回通讯页面")
        return AddressBookPage(self.driver)


    @allure.step("添加成员失败")
    def add_menger_save_fail(self,name,mid,phone_num):
        self.input(*self._NAME_LOCATOR,name)
        self.input(*self._MID_LOCATOR,mid)
        self.input(*self._PHONE_LOCATOR,phone_num)
        self.click(*self._SAVE_BTN)
        print("添加成员失败，停留在原页面")
        return self


    def get_tips(self):
        tips = self.find_ele(*self._FAIL_TIPS).text
        print(f"获取tips提示信息：{tips}")
        return tips


    def click_cancle(self):
        self.click(*self._CANCEL_BTN)
        print("添加成员失败，点击取消，返回通讯页面")
        return AddressBookPage(self.driver)